﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<客户档案查看日志>
-- =============================================
CREATE PROCEDURE [dbo].[proc_Customer_View_Log_GetList]
(
	@StartIndex int,
	@EndIndex INT,
	@CompanyId INT,
	@TypeId INT,
	@Stext NVARCHAR(200),
	@StartDate NVARCHAR(10),
	@EndDate NVARCHAR(10)
)
AS
BEGIN
	Declare @sRq Datetime,@eRq Datetime
	Set @sRq = Convert(Datetime,@StartDate+' 00:00:00')
	Set @eRq = Convert(Datetime,@EndDate+' 23:59:59')
	IF @TypeId=0
	BEGIN
        ;WITH List As(Select ROW_NUMBER() OVER (Order BY UpTime Desc) AS Row,
		id,
		CusName,
		UserId,
		UserName,
		ISNULL((SELECT c.CompanyName FROM Company c WHERE c.Id=l.CompanyId),'') AS CompanyName,
		UpTime, 
		ISNULL((SELECT p.Title FROM Customer_Protect p WHERE p.Id=l.ProtectId),'') AS ProtectTitle,
		ProtectUser,
		ProtectName,
		ProtectDate
	    FROM Customer_View_Log l
	    WHERE
	    (@CompanyId=0 Or CompanyId=@CompanyId)
	    And UpTime>=@sRq And UpTime<=@eRq
		And (@Stext='' Or l.CusName LIKE '%'+@Stext+'%')
		)

		Select *,(Select Count(0) From list) As RecordCount From list Where Row Between @StartIndex and @EndIndex Order By Row
	END
	ELSE
		BEGIN
		;WITH List As(Select ROW_NUMBER() OVER (Order BY UpTime Desc) AS Row,
		id,
		CusName,
		UserId,
		UserName,
		ISNULL((SELECT c.CompanyName FROM Company c WHERE c.Id=l.CompanyId),'') AS CompanyName,
		UpTime, 
		ISNULL((SELECT p.Title FROM Customer_Protect p WHERE p.Id=l.ProtectId),'') AS ProtectTitle,
		ProtectUser,
		ProtectName,
		ProtectDate
	    FROM Customer_View_Log l
	    WHERE
	    (@CompanyId=0 Or CompanyId=@CompanyId)
	    And UpTime>=@sRq And UpTime<=@eRq
		And (@Stext='' Or UserName LIKE '%'+@Stext+'%')
		)
		Select *,(Select Count(0) From list) As RecordCount From list Where Row Between @StartIndex and @EndIndex Order By Row
		END	
END
